package com.clz.hyperit.service.impl;


import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.clz.hyperit.dao.CollectionDao;
import com.clz.hyperit.dao.FollowDao;
import com.clz.hyperit.dao.GoodsDao;
import com.clz.hyperit.dao.NoteDao;
import com.clz.hyperit.dao.UserDetailDao;
import com.clz.hyperit.entity.Article;
import com.clz.hyperit.entity.Collection;
import com.clz.hyperit.entity.Note;
import com.clz.hyperit.entity.UserDetail;
import com.clz.hyperit.exception.ApplicationRunTimeException;
import com.clz.hyperit.service.NoteService;
import com.clz.hyperit.vo.PageInfo;
@Service("noteService")
public class NoteServiceImpl implements NoteService {
	private static Logger logger = LoggerFactory
			.getLogger(GoodsServiceImpl.class);

	@Autowired
	private NoteDao noteDao;
	
	@Autowired
	private UserDetailDao userdetailDao;
	
	@Autowired
	private CollectionDao collectionDao;
	@Autowired
	private FollowDao followDao;
	
	public Integer saveNote(Note note) {
		UserDetail ud = userdetailDao.get(note.getUserDetail_id());
		if(note.getStatus()==0){
			ud.setNote(ud.getNote()+1);
		}
		note.setReading(0);
		note.setCollecting(0);
		userdetailDao.updateWithNotNullProperties(ud);
		note.setUserDetail_id(ud.getId());
		return noteDao.save(note);
	}

	public Note getbyid(Integer note_id) {
		return noteDao.get(note_id);
	}

	public void deleteNote(Integer note_id, Integer userdetail_id) {
		Note note = noteDao.get(note_id);
		//用户关联
		UserDetail ud = userdetailDao.get(userdetail_id);
		if(note.getStatus()==0){
			ud.setNote(ud.getNote()-1);
			userdetailDao.updateWithNotNullProperties(ud);
		}
		//收藏关联
		collectionDao.deleteByNoteId(note_id);
		noteDao.deleteById(note_id);
	}

	public Note updateNote(Note note, Integer userdetail_id) {
		Note note_1=noteDao.get(note.getId());
		if(userdetail_id!=note_1.getUserDetail_id()){
			throw new ApplicationRunTimeException("没有权限操作");
		}
		//更新article的信息
		note_1.setContent_md(note.getContent_md());
		note_1.setContent_html(note.getContent_html());
		note_1.setTitle(note.getTitle());
		note_1.setStatus(note.getStatus());
		noteDao.updateWithNotNullProperties(note_1);
	
		return noteDao.get(note.getId());
	}



	public List<Note> findNote(PageInfo pageInfo) {
		return noteDao.findAll(pageInfo);
	}

	public boolean getCollection(Integer note_id, Integer userdetail_id) {
		Collection collection = collectionDao.getbyNoteAndUd(note_id,userdetail_id);
		if(collection==null){
			return false;
		}
		else{
			return true;
		}
	}

	public boolean getFollow(Integer target_id, Integer userdetail_id) {
		if(followDao.get_userid_targetid(userdetail_id,target_id)==null){
			return false;
		}
		else{
			return true;
		}
	}

	public List<Note> findPersonNote(Integer userdetail_id, Integer status,PageInfo pageInfo) {
		return  noteDao.findPersonNote(userdetail_id,status,pageInfo);	
	}

	public List<Note> findNoteByRead(PageInfo pageInfo,Integer status) {
		return noteDao.findNoteByRead(pageInfo,status);
	}

	public List<Note> findNoteByCollect(PageInfo pageInfo, Integer status) {
		return noteDao.findNoteByCollect(pageInfo,status);
	}

	public List<Note> searchNote(PageInfo pageInfo, String search,Integer status) {
		return noteDao.findNoteBysearch(pageInfo,search,status);
	}

	public void updateNote(Note note) {
		Note note_1=noteDao.get(note.getId());
		//更新article的信息
		note_1.setContent_md(note.getContent_md());
		note_1.setContent_html(note.getContent_html());
		note_1.setTitle(note.getTitle());
		note_1.setStatus(note.getStatus());
		noteDao.updateWithNotNullProperties(note_1);
		
	}



	
}
